package com.mrd.util;

import com.mrd.annotation.validation.DeclarationLengthGroup;
import com.mrd.annotation.validation.InDeclarationRequiredGroup;
import com.mrd.annotation.validation.OutDeclarationRequiredGroup;
import com.mrd.exception.validation.MRDValidationException;
import com.mrd.gtimp.business.entity.Commodity;
import com.mrd.gtimp.business.entity.Declaration;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.groups.Default;

/**
 * 验证工具 Created by dengdc on 2016/10/19.
 */
public class ValidationUtil {

    public enum Groups{
        in,out;
        public Class value(){
            if(this.name().equals(in.toString())){
                return InDeclarationRequiredGroup.class;
            }else{
                return OutDeclarationRequiredGroup.class;
            }
        }
    }

    /**
     * 对象验证（字段验证）
     */
    public static void validate(Object t) {
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(t);
        collectErrorMessage(constraintViolations);
    }

    /**
     * 收集错误信息
     */
    private static void collectErrorMessage(Set<ConstraintViolation<Object>> constraintViolations) {
        if (constraintViolations.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (ConstraintViolation constraintViolation : constraintViolations) {
                System.out.println(constraintViolation.getMessage());
                sb.append(constraintViolation.getMessage()).append(", ");
            }
            sb.delete(sb.length() - 2, sb.length() - 1);
            throw new MRDValidationException(sb.toString());
        }
    }

    /**
     * 对象验证（字段验证）, 指定验证组
     */
    public static void validateByGroup(Object t, Groups group) {
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(t, group.value());
        collectErrorMessage(constraintViolations);
    }

    public static Class[] getDefault(){
        return new Class[]{Default.class, DeclarationLengthGroup.class, InDeclarationRequiredGroup.class, OutDeclarationRequiredGroup.class};
    }

    public static void main(String[] args) {
        Declaration d = new Declaration();
        d.setState("");
//        d.setId("666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666");
        List<Commodity> commodityList = new ArrayList<>();
        Commodity commodity = new Commodity();
//        commodity.setCode_s("222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222");
        commodityList.add(commodity);
        d.setCommodities(commodityList);
//        LocalValidatorFactoryBean validatorFactory = new LocalValidatorFactoryBean();
//        validatorFactory.setTraversableResolver(new MRDTraversableResolver());
//        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
//        Validator validator = validatorFactory.getValidator();
//        Set<ConstraintViolation<Declaration>> constraintViolations = validator.validate(d);
//        for (ConstraintViolation<Declaration> constraintViolation : constraintViolations) {
//            System.out.println(constraintViolation.getMessage());
//        }
//        System.out.println(constraintViolations);
        System.out.println("------------------------------");
        ValidationUtil.validateByGroup(d, Groups.in);
    }
}
